<?php
/**
 * message element controller
 *
 * @abstract
 * @author raphael seebacher <raphasee@ee.ethz.ch>
 * @version 0.01
 */
abstract class message_element_controller extends controller {
    protected $content;
    protected $parameters;

    public function __construct($message) {
        if (!(is_array($message))
            || !(array_key_exists($this->get_tag(), $message))
        ) {
            throw new no_element_exception($this->get_tag());
        }
        else {
            $this->content = $message[$this->get_tag()];
        }
    }

    public function get_response() {
        try {
            $element_content = $this->process_element();
        }
        catch (error $e) {
            $element_content = $e->getResponse();
        }
        catch (warning $e) {
            $element_content = $e->getResponse();
        }

        $element_response = Array(
          $this->get_tag() => $element_content,
        );

        return $element_response;
    }

    protected function check_content_parameters_all_present() {
        $missing_parameters = Array();

        foreach ($this->parameters as $parameter) {
            if (!array_key_exists($parameter, $this->content)) {
                $missing_parameters[] = $parameter;
            }
        }

        if (count($missing_parameters)) {
            throw new missing_parameters($missing_parameters);
        }
    }

    /**
     * method to process a specific element of the message object
     * 
     * @abstract
     * @return array an associative array that contains the response
     */
    abstract protected function process_element();

    /**
     * method to determine the tag of a message element
     * 
     * @abstract
     * @return string the tag used to identify data provided for the specific
     * message element
     */
    abstract public function get_tag();
}
?>